﻿using System;
using System.Linq;
using Pap.Data;
using Pap.Data.Abstraction.Utils;


namespace Pap.Services.Customer
{
    /// <summary>
    /// 
    /// </summary>
    public class PeriodFromLastLoan : CustomerValidatorBase
    {
        /// <summary>
        /// 
        /// </summary>
        IAdministrationRepository administrationRepository;
        /// <summary>
        /// Initializes a new instance of the <see cref="PeriodFromLastLoan"/> class.
        /// </summary>
        /// <param name="_db">The _DB.</param>
        /// <param name="adminRepo">The admin repo.</param>
        public PeriodFromLastLoan(IDatabase _db, IAdministrationRepository adminRepo)
            : base(_db)
        {
            isError = false;
            canValidateByName = true;
            this.administrationRepository = adminRepo;
        }

        /// <summary>
        /// Validates the specified current committee ID.
        /// </summary>
        /// <param name="currentCommitteeID">The current committee ID.</param>
        /// <param name="customer">The customer.</param>
        /// <returns></returns>
        public override bool Validate(Guid currentCommitteeID, Pap.Data.ICustomer customer)
        {
            int allowedDaysFromLastClosedLoan = int.Parse(administrationRepository.GetSettingValue(currentCommitteeID, SettingKeys.AllowedDaysFromLastClosedLoan));

            var lastClosedLoan = (from loan in _database.CustomerLoans
                                  join history in _database.CustomerLoanHistories
                                  on loan.LoanID equals history.LoanID
                                  where loan.CustomerID == customer.CustomerID
                                  select history).Max(h => h.LoanCloseDate);

            if (lastClosedLoan == null)
            {
                return true;
            }
            else
            {
                TimeSpan elapsedTime = DateTime.Now - (DateTime)lastClosedLoan;
                if (elapsedTime.Days > allowedDaysFromLastClosedLoan)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        /// <summary>
        /// Validates the specified current committee ID.
        /// </summary>
        /// <param name="currentCommitteeID">The current committee ID.</param>
        /// <param name="customerName">Name of the customer.</param>
        /// <returns></returns>
        public override bool Validate(Guid currentCommitteeID, string customerName)
        {
            int allowedDaysFromLastClosedLoan = int.Parse(administrationRepository.GetSettingValue(currentCommitteeID, SettingKeys.AllowedDaysFromLastClosedLoan));

            var lastClosedLoan = (from history in _database.CustomerLoanHistories
                                  where history.CustomerName.Contains(customerName)
                                  select history).Max(h => h.LoanCloseDate);

            if (lastClosedLoan == null)
            {
                return true;
            }
            else
            {
                TimeSpan elapsedTime = DateTime.Now - (DateTime)lastClosedLoan;
                if (elapsedTime.Days > allowedDaysFromLastClosedLoan)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
    }
}
